<template name="sendcar">
	<!-- 物资单列表 做入库申请单 -->
	<!-- 物资列表主要有2种 ，库存物资-> 出库业务   基础物资->入库业务  -->
	<view class="mypage">
		<view class="cu-bar search bg-white">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入物资名称" v-model="keyword" />
				<input type="text" placeholder="请输入物资规格" v-model="keyword2" />
			</view>
			<view class="action">
				<button class="cu-btn bg-blue shadow-blur round" @tap="handelSearch">搜索</button>
			</view>
		</view>

		<view class="card-list-wrapper">
			<view class="card-list-row radius shadow-warp bg-white margin-top" v-for="(item,index) in planList"
				:key="index" @click="openDetail(index)">
				<view class="flex-just-between bottom-border top-wrap">
					<text class="text-blue text-bold">物资单号：{{ item.PK_MATERIAL || item.BILLNO }}</text>
					<view class="cu-capsule radius">
						<view class="cu-tag bg-blue sm">
							<text class="cuIcon-medal"></text>
						</view>
						<view class="cu-tag line-brown sm">{{ index+1}}</view>
					</view>
				</view>

				<view class="card-list-row-1 margin-top">
					<text class="cuIcon-title text-red"></text>
					<text>名称：{{ item.MATERIALNAME || item.VNAME || '' }}</text>
				</view>

				<view class="card-list-row-1">
					<text class="cuIcon-title text-orange"></text>
					<text>规格：{{ item.MATSPEC || '' }}</text>
				</view>
				
				<view class="card-list-row-1">
					<text class="cuIcon-title text-orange"></text>
					<text>计量单位：{{ item.MINUNITNAME || '' }}</text>
				</view>
				
				

				<view class v-if="category=='ST_STOCKINFO' ">
					<view class="card-list-row-1">
						<text class="cuIcon-title text-orange"></text>
						<text>库存总量：{{ item.TOTALNUM  }}({{ item.UNIT || '' }})</text>
					</view>
					<view class="card-list-row-1" v-for="(itemS,indexS) in mapSattionName(item.VBDEF18)">
						<text class="cuIcon-title text-red"></text>
						<text>{{ indexS+1 }}号货位：{{ itemS.STATIONNAME || '' }}/{{ itemS.KCNUM }} {{ item.UNIT }}</text>
					</view>
				</view>

				<view class="position-a checkFlag" v-if="item.ischeck">
					<text class="cuIcon-squarecheckfill text-blue"></text>
				</view>
			</view>
		</view>

		<view class="next-btn-wrap position-f" @click="nextTap">
			<button class="big-btn bg-green shadow-blur">
				<text class="cuIcon-refresharrow text-bold"></text>
			</button>
		</view>

		<modal title="高级查询" placeholder :isShow="isShowModal" @cancelModal="cancelModal" @okModal="okModal"></modal>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import api from "@/common/api.js";
	import utils from "@/common/util.js";
	import modal from "@/components/modal/modal_mater.vue";
	let query = {
		category: "",
		page: 1,
		rows: "10",
		sidx: "CREATIONTIME",
		sord: "desc",
		queryJson: "",
		TYPE: "",
		userId: "",
		account: ""
	};
	let _self,
		menuType,
		page = 1,
		billID,
		total = 1,
		goPath = "",
		pageTitle = "基础物资列表(",
		queryJson = {};

	export default {
		computed: mapState(["userInfo", "baseUrl", "checkData"]),
		data() {
			return {
				planList: [],
				keyword: "",
				keyword2: "",
				category: "",
				isShowModal: false,
				config: {
					materId: "",
					materName: ""
				}
			};
		},

		onLoad(p) {
			_self = this;
			this.setCheckData([]);
			menuType = p.sqlTbName;
			this.initData();
			// 刷新列表
			uni.$once("refreshList", () => {
				page = 1;
				total = 1;
				queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
				this.getList();
			});
		},
		components: {
			modal
		},
		onNavigationBarButtonTap(e) {
			// this.isShowModal = true;
			// if(e.index==0)
			e.index == 0 ? (this.isShowModal = true) : this.openScanMater();
			// console.log(e);
		},
		onShow() {

		},
		//下拉刷新
		onPullDownRefresh: function() {
			// 重置分页及数据
			page = 1;
			total = 1;
			queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
			// queryJson.account = _self.userInfo.Account
			_self.planList = [];
			// queryJson = "";
			this.getList();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
			uni.showToast({
				title: "数据已刷新",
				icon: "none"
			});
		},
		// 加载更多
		onReachBottom: function() {
			this.getList();
		},
		methods: {
			...mapMutations(["setCheckData"]),
			initData() {
				if (menuType == "applyIn" || menuType == "manualIn") {
					query.category = "BD_MATERIAL";
				} else if (menuType == "applyOut" || menuType == "manualOut") {
					query.category = "ST_STOCKINFO";
					query.TYPE = "ALL"
					pageTitle = "库存物资列表(";
				}
				// 判断要跳转的 MATSPEC
				goPath = "/pages/tab01/inbound/submit-manual?data=";
				switch (menuType) {
					case "applyIn": // 新增入库申请单
						goPath = "/pages/tab01/inbound/submit-apply?data=";
						break;
					case "manualIn": // 手工入库
						goPath = "/pages/tab01/inbound/submit-manual?data=";
						break;
					case "applyOut": // 新增 出库申请单
						goPath = "/pages/tab01/outbound/submit-apply?data=";
						break;
					case "manualOut": // 手工出库单
						goPath = "/pages/tab01/outbound/submit-manual?data=";
						break;
					default:
						break;
				}
				// 手工单出库 + 新增出库申请单 使用库存物资
				query.userId = _self.userInfo.UserId;
				query.account = _self.userInfo.Account;
				queryJson.PK_ORG = _self.userInfo.ORGANIZEID;
				this.category = query.category;
				page = 1;
				total = 1;
				this.getList();
			},

			// STATIONNAME 遍历仓库

			mapSattionName(data) {
				if (!data) return [];
				let arr = JSON.parse(data);
				return arr;
			},

			// 扫描物资
			openScanMater() {
				let URL =  api.apiPublic.ScanCode;
				this.$utils.Scan(URL, item => {
					
					if(item.PK_STATION  && !item.PK_MATERIAL ){
						// console.log('扫码物资', item );
						page = 1
						return _self.getList(item  )
					}
					// console.log( 333, item );
					
					item.ischeck = false;
					item.FACTORYDATE = "";
					item.EXPDATE = "180";
					(item.NNUMSQ = item.NNUM), (item.TOTALNUM = item.TOTALNUM);
					item.NNUM = '';
					item.INSTOCKDATE = utils.getToDay();
					item.STORENAME = "";
					item.PK_STORE = "";
					item.STATIONNAME = "";
					item.PK_STATION = "";
					item.INPHOTO = "";
					(item.OUTPHOTO = ""), (item.VBDEF20 = "");
					item.myindex01 = 0;
					item.myindex02 = 0;
					item.NTAXRATE = "";
					item.PURPOSE = "";
					item.ISSURE = false // 物资是否扫码确认
					item.flagText = '未扫码确认请扫码确认'
					item.PK_MATERIAL = item.PK_MATERIAL || item.BILLNO
					_self.planList = [];
					_self.planList.push(item);
				});
			},

			openDetail(e) {
				_self.planList[e].ischeck = !_self.planList[e].ischeck;
				_self.planList[e].myIndex = e;
			},
			handelSearch() {
				// queryJson.BILLNO = _self.keyword;
				let a = {
					MATSPEC: _self.keyword2,
					MATERIALNAME: _self.keyword,
					VNAME: _self.keyword,
					BILLNO: "",
					PK_ORG: _self.userInfo.ORGANIZEID
				};
				queryJson = {
					...a
				};
				page = 1;
				total = 1;
				_self.planList = [];
				this.getList();
			},
			nextTap() {
				let t = [];
				_self.planList.forEach(item => {
					if (item.ischeck) {
						t.push(item);
					}
				});
				let newCheckData = []; // 如果新选择的数据不存在 已选择中 进行添加到 chekData
				let oldCheckDataPK = '';
				_self.checkData.forEach(item => {
					oldCheckDataPK += item.PK_MATERIAL + '-';
				})

				if (_self.checkData.length == 0) {
					newCheckData = [...t]
				} else {
					t.forEach(item => {
						if (oldCheckDataPK.indexOf(item.PK_MATERIAL) < 0) {
							newCheckData.push(item)
						}
					})
				}
				let newCheckData2 = [...newCheckData, ...this.checkData];
				this.setCheckData(newCheckData2);
				if (newCheckData2.length < 1) return _self.$utils.tips("请至少选择一个物资！");
				uni.navigateTo({
					url: goPath + encodeURIComponent(JSON.stringify(t))
				});
			},

			getList(scanData) {
				if (page > total) {
					return utils.tips("没有数据了");
				}
				query.page = page;
				query.account = _self.userInfo.Account
				query.queryJson = JSON.stringify(queryJson);
				if( scanData ){
					let queryJson = { PK_STATION: scanData.PK_STATION  }
					query.queryJson = JSON.stringify(queryJson);
					
					// let mQuery = JSON.parse(JSON.stringify(query));
					// mQuery.category = 'ST_STOCKINFO'
					// mQuery.queryJson =  JSON.stringify(queryJson);
				}
				// console.log(query);
				let URL =  api.apiPublic.getMaterList;
				// console.log(URL);
				this.axios({
						url: URL,
						data: query,
						isLoading:1
					})
					.then((res) => {
						let {
							IsSuccess,
							Result,
							Message
						} = res;
						
						console.log('物资列表', Result );
						let array = Result.rows.map(item => {
							item.ischeck = false;
							item.FACTORYDATE = "";
							item.EXPDATE = "180";
							(item.NNUMSQ = item.NNUM), (item.TOTALNUM = item.TOTALNUM);
							item.NNUM = '';
							item.MATCODE = item.VCODE ;
							item.INSTOCKDATE = utils.getToDay();
							item.STORENAME = "";
							item.PK_STORE = "";
							item.STATIONNAME = "";
							item.PK_STATION = "";
							item.INPHOTO = "";
							(item.OUTPHOTO = ""), (item.VBDEF20 = "");
							item.myindex01 = 0;
							item.myindex02 = 0;
							item.NTAXRATE = "";
							item.PURPOSE = "";
							item.ISSURE = false // 物资是否扫码确认
							item.flagText = '未扫码确认请扫码确认'
							item.PK_MATERIAL = item.PK_MATERIAL || item.BILLNO
							
							item.MEANSNAME =  item.MEANSNAME || ''
							item.PK_MEANS = item.PK_MEANS || ''
							item.ISMEANS = item.ISMEANS || ''
							item.NEXTPAIRDATE = item.NEXTPAIRDATE || ''
							
							item.EQUIPNAME = item.EQUIPNAME || ''
							item.PK_EQUIP = item.PK_EQUIP || ''
							
							return item;
						});
						if (page == 1) {
							_self.planList = array;
							if (_self.planList.length == 0) {
								utils.tips("暂无数据！");
							}
							_self.setPageTitle(pageTitle + Result.records + ")");
						} else {
							_self.planList = _self.planList.concat(array);
						}
						queryJson = {};
						total = Result.total;
						page++;
					})
			},
			setPageTitle(d) {
				uni.setNavigationBarTitle({
					title: d
				});
			},
			// 控制高级查询条件
			cancelModal() {
				this.isShowModal = false;
			},
			okModal(val) {
				this.cancelModal();
				queryJson = {
					...val
				};
				page = 1;
				total = 1;
				_self.planList = [];
				this.getList();
			}
		}
	};
</script>

<style>
	page {
		padding-bottom: 100rpx;
		box-sizing: border-box;
	}

	.card-list-wrapper {
		width: 710upx;
		margin: 0 auto;
	}

	.card-list-row {
		padding: 20upx;
	}

	.card-list-row-1 {
		padding: 10upx 0;
	}



	.top-border {
		border-top: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.top-wrap {
		padding-bottom: 20upx;
	}

	.bottom-border {
		border-bottom: 1upx solid rgba(0, 0, 0, 0.1);
	}

	.btn-wrap {
		padding-top: 20upx;
	}

	.checkFlag {
		top: 50%;
		right: 20upx;
		font-size: 40upx;
	}

	.next-btn-wrap {
		position: fixed;
		right: 50upx;
		bottom: 100upx;
	}

	.big-btn {
		width: 100upx;
		height: 100upx;
		border-radius: 50upx;
		font-size: 40upx;
	}
</style>
